<?php

/**
 * Created by PhpStorm.
 * User: GadflyBSD
 * Date: 16/3/12
 * Time: 11:36
 */
class ApiAction extends CommonAction{
	/**
	 * 微信通用推送接口
	 *
	 * @param   url     API地址: http://wx.mywoka.com/Api/pushMsg.html
	 * @param   weixin  POST方式传递向哪个微信号进行信息推送,可选: WKSHOP(汪卡商户)和WAKA(汪卡),默认为:WKSHOP
	 * @param   openid  POST方式传递需推送用户的openid
	 * @param   msg     POST方式传递需推送的正文部分,
	 *
	 * @return  返回值    在调用模板消息推送接口后，会返回JSON数据包。正常时的返回JSON数据包示例：
	 *                   {"errcode":0, "errmsg":"ok", "msgid":200228332}
	 *                   其他返回码参考: http://mp.weixin.qq.com/wiki/17/fa4e1434e57290788bde25603fa2fcbd.html
	 */
	public function pushMsg(){
		if($this->defaultHeader('POST')){
			$param = defaultParam(I('post.weixin'), array('WKSHOP', 'WAKA'), 'WKSHOP');
			$pushmsg = D('Pushmsg')->message($param, I('post.openid'), I('post.msg'));
			$this->ajax($pushmsg);
		}
	}

	/**
	 * 微信模板信息推送接口
	 * @param   url             API地址: http://wx.mywoka.com/Api/pushTemplateMsg.html
	 * @param   weixin          POST方式传递向哪个微信号进行信息推送,可选: WKSHOP(汪卡商户)和WAKA(汪卡),默认为:WKSHOP
	 * @param   openid          POST方式传递需推送用户的openid
	 * @param   template_id     POST方式传递需推送的模板编号,
	 * @param   data            POST方式传递需推送的模板数据,JSON格式数据
	 *
	 * @return mixed    在调用模板消息推送接口后，会返回JSON数据包。正常时的返回JSON数据包示例：
	 *                   {"errcode":0, "errmsg":"ok", "msgid":200228332}
	 *                  其他返回码参考: http://mp.weixin.qq.com/wiki/17/fa4e1434e57290788bde25603fa2fcbd.html
	 */
	public function pushTemplateMsg(){
		if($this->defaultHeader('POST')){
			switch(I('post.type')){
				case 'confirm':
					/**
					 * 发送订单确认提醒
					 * @param $post['type']         APP端POST方式传递过来的推送类型, 此处为: confirm
					 * @param $post['app_primary']  APP端POST方式传递过来的订单编号,或订单表主键
					 * @param $post['wz_primary']   APP端POST方式传递过来的微信端用户uid值
					 * @param $post['money']        APP端POST方式传递过来的订单金额
					 */
					$pushmsg = D('Pushmsg')->template(array(
						'type'  => 'confirm',
						'id'    => I('post.app_primary'),
						'uid'   => I('post.wz_primary'),
						'money' => I('post.money'),
						'mobile'=> I('post.mobile'),
					));
					break;
				case 'survey':
					/**
					 * 发送尽调员尽职调查提醒
					 * @url                         API地址: http://wx.mywoka.com/Api/pushTemplateMsg.html
					 * @param $post['type']         APP端POST方式传递过来的推送类型, 此处为: survey
					 * @param $post['app_primary']  APP端POST方式传递过来的新注册用户编号,或用户表主键(需要尽职调查的大学生ID)
					 * @param $post['wz_primary']   APP端POST方式传递过来的微信端用户uid值(尽调员UID)
					 */
					$pushmsg = D('Pushmsg')->template(array(
						'type'  => 'survey',
						'id'    => I('post.id'),
						'uid'   => I('post.uid'),
					));
					break;
			}
			$this->ajax($pushmsg);
		}
	}

	public function Pnr(){
		Vendor('ChinaPnr');
		$pnr = new ChinaPnr();
		$return = $pnr->callback(I('post.'));
		if($return['errcode'] = 0){
			switch($return['data']['cmd_id']){
				case '201':
					if($return['data']['resp_code'] = '201000')
						D('Customer')->rechargeSuccess($return['data']);
					else
						D('Customer')->rechargeFail($return['data']);
					break;
				case '202':
					if($return['data']['resp_code'] = '202000')
						D('Business')->withdrawSuccess($return['data']);
					else
						D('Business')->withdrawFail($return['data']);
					break;
			}
			log::write($return['recv'], 'CallBack-RECV');
			echo $return['recv'];
		}else{
			log::write(json_encode_plus($return), 'CallBack-ERROR');
			dump($return);
		}
	}

	private function defaultHeader($request='ALL'){
		header("Content-type: json; charset=utf-8");
		header('Access-Control-Allow-Origin:*');
		header('Access-Control-Allow-Headers:x-requested-with,content-type');
		switch($request){
			case 'GET':
				header('Access-Control-Allow-Methods:GET');
				return $this->isGet();
			case 'POST':
				header('Access-Control-Allow-Methods:POST');
				return $this->isPost();
			case 'PUT':
				header('Access-Control-Allow-Methods:PUT');
				return $this->isPut();
			case 'DELECT':
				header('Access-Control-Allow-Methods:DELETE');
				return $this->isDelete();
			case 'ALL':
				header('Access-Control-Allow-Methods:POST, GET, PUT, DELETE');
				return ($this->isGet() || $this->isPost() || $this->isPut() || $this->isDelete())?true:false;
			default:
				return false;
		}
	}

}